package io.gitee.zhangbinhub.admin.common.base

import io.gitee.zhangbinhub.acp.boot.log.LogAdapter
import io.gitee.zhangbinhub.admin.common.constant.BusEventConstant
import io.gitee.zhangbinhub.admin.common.event.*
import org.noear.snack4.ONode
import org.noear.solon.cloud.CloudEventHandler
import org.noear.solon.cloud.model.Event

abstract class BaseCloudEventHandler(private val logAdapter: LogAdapter) : CloudEventHandler {
    @Throws(Throwable::class)
    override fun handle(event: Event): Boolean {
        try {
            when (event.topic()) {
                BusEventConstant.EXECUTE_BUS_EVENT_TOPIC -> {
                    doHandle(
                        ExecuteBusEvent(ONode.deserialize(event.content(), CloudBusEventContent::class.java))
                    )
                }

                BusEventConstant.NOTICE_BUS_EVENT_TOPIC -> {
                    doHandle(
                        NoticeBusEvent(ONode.deserialize(event.content(), CloudBusEventContent::class.java))
                    )
                }

                BusEventConstant.RELOAD_DATA_BUS_EVENT_TOPIC -> {
                    doHandle(
                        ReloadDataBusEvent(ONode.deserialize(event.content(), CloudBusEventContent::class.java))
                    )
                }

                BusEventConstant.ROUTE_LOG_BUS_EVENT_TOPIC -> {
                    doHandle(
                        RouteLogBusEvent(ONode.deserialize(event.content(), CloudBusEventContent::class.java))
                    )
                }
            }
        } catch (e: Exception) {
            logAdapter.error(e.message, e)
        }
        return true
    }

    open fun doHandle(executeBusEvent: ExecuteBusEvent) {}
    open fun doHandle(noticeBusEvent: NoticeBusEvent) {}
    open fun doHandle(reloadDataBusEvent: ReloadDataBusEvent) {}
    open fun doHandle(routeLogBusEvent: RouteLogBusEvent) {}
}